package com.nulldev.util.java.swing.vdesktop;

import javax.swing.JInternalFrame;

import java.awt.Color;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.nulldev.util.os.BIOSUtil.BIOS;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.os.BIOSUtil;
import com.nulldev.util.os.CPUUtil;
import com.nulldev.util.os.GPUUtil;
import com.nulldev.util.os.CPUUtil.CPU;
import com.nulldev.util.os.GPUUtil.GPU;
import com.nulldev.util.os.OSUtil.Windows_Type;
import com.nulldev.util.os.OSUtil;
import com.nulldev.util.other.ArrayUtils;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class vSecEngine extends JInternalFrame {

	/**
	 * Launch the application.
	 * @return 
	 */
	public static vSecEngine main() {
		final vSecEngine frame = new vSecEngine();
		ThemeApplier.applyTheme(frame);
		frame.setVisible(true);
		return frame;
	}

	/**
	 * Create the frame.
	 */
	public vSecEngine() {
		setTitle("SecurityEngine");
		setResizable(true);
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 450, 440);
		
		JLabel lblCpuType = new JLabel("CPU Type:");
		
		JLabel lblNa = new JLabel("N/A") {
			@Override
			public void setForeground(final Color color) {
				if (color != Color.green && color != Color.RED && color != Color.ORANGE) {
					return;
				}
				super.setForeground(color);
			}
		};
		
		final String cpuArch = CPUUtil.getArch();
		
		lblNa.setText(cpuArch);
		if (cpuArch.equals("amd64")) {
			lblNa.setForeground(Color.ORANGE);
		}else if (cpuArch.equals("x86")) {
			lblNa.setForeground(Color.ORANGE);
		}else {
			lblNa.setForeground(Color.GREEN);
		}
		
		JLabel label = new JLabel("CPU Bugs:");
		
		JLabel lblSafe = new JLabel("SAFE") {
			@Override
			public void setForeground(final Color color) {
				if (color != Color.green && color != Color.RED) {
					return;
				}
				super.setForeground(color);
			}
		};
		lblSafe.setForeground(Color.green);
		
		final String[] bugs = CPUUtil.getBugs();
		if (bugs.length > 0) {
			ArrayUtils.println("CPU.getBugs() -> ", bugs);
			lblSafe.setText(bugs.length + " bugs found.");
			lblSafe.setForeground(Color.RED);
		}
		
		JLabel lblBackdoors = new JLabel("Backdoors:");
		
		JLabel lblSafe_1 = new JLabel("SAFE") {
			@Override
			public void setForeground(final Color color) {
				if (color != Color.green && color != Color.RED && color != Color.ORANGE) {
					return;
				}
				super.setForeground(color);
			}
		};
		lblSafe_1.setForeground(Color.green);
		
		final String[] bck = CPUUtil.getBackdoors();
		
		if (bck != null && bck.length > 0) {
			ArrayUtils.println("CPU.getBackdoors() -> ", bck);
			if (bck.length == 1) {
				lblSafe_1.setText(bck.length + " backdoor found.");
			}else {
				lblSafe_1.setText(bck.length + " backdoors found.");
			}
			lblSafe_1.setForeground(Color.RED);
		}
		
		JLabel lblVirtualMachine = new JLabel("Virtual Machine:");
		
		final CPU cpu = CPUUtil.parse();
		JLabel lblNo = new JLabel(cpu.isVM() ? cpu.getVMVendor().name() : "NO");
		
		JLabel lblOs = new JLabel("OS:");
		
		JLabel lblNa_1 = new JLabel("N/A") {
			@Override
			public void setForeground(final Color color) {
				if (color != Color.green && color != Color.RED && color != Color.ORANGE) {
					return;
				}
				super.setForeground(color);
			}
		};
		
		if (OSUtil.isWindows() || OSUtil.isMac()) {
			lblNa_1.setForeground(Color.ORANGE);
		}else {
			lblNa_1.setForeground(Color.GREEN);
		}
		
		if (OSUtil.isWindows()) {
			final Windows_Type wt = Windows_Type.getType();
			if (wt.getNT() > 5.2) {
				lblNa_1.setForeground(Color.RED);
			}else {
			}
			lblNa_1.setText(wt.getNTString());
		}else {
			lblNa_1.setText(OSUtil.getOperatingSystemString());
		}
		
		JLabel lblBios = new JLabel("BIOS:");
		
		final BIOS bios = BIOSUtil.parse();
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblCpu = new JLabel("CPU:");
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		JLabel lblGpu = new JLabel("GPU:");
		
		JScrollPane scrollPane_2 = new JScrollPane();
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
						.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCpuType)
							.addPreferredGap(ComponentPlacement.RELATED, 299, Short.MAX_VALUE)
							.addComponent(lblNa))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED, 310, Short.MAX_VALUE)
							.addComponent(lblSafe))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblBackdoors)
							.addPreferredGap(ComponentPlacement.RELATED, 303, Short.MAX_VALUE)
							.addComponent(lblSafe_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblVirtualMachine)
							.addPreferredGap(ComponentPlacement.RELATED, 280, Short.MAX_VALUE)
							.addComponent(lblNo))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblOs)
							.addPreferredGap(ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
							.addComponent(lblNa_1))
						.addComponent(lblCpu, Alignment.LEADING)
						.addComponent(lblBios, Alignment.LEADING)
						.addComponent(lblGpu, Alignment.LEADING))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCpuType)
						.addComponent(lblNa))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(lblSafe))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBackdoors)
						.addComponent(lblSafe_1))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVirtualMachine)
						.addComponent(lblNo))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNa_1)
						.addComponent(lblOs))
					.addGap(12)
					.addComponent(lblBios)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblCpu)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblGpu)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane_2, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		final GPU gpu = GPUUtil.parse();
		JTextPane textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		textPane_1.setText(gpu.toString());
		scrollPane_2.setViewportView(textPane_1);
		
		JTextPane textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setText(cpu.toString());
		scrollPane_1.setViewportView(textPane);
		
		JTextArea txtrA = new JTextArea();
		scrollPane.setViewportView(txtrA);
		txtrA.setLineWrap(true);
		txtrA.setTabSize(16);
		txtrA.setEditable(false);
		txtrA.setText(bios.toString());
		getContentPane().setLayout(groupLayout);

	}
}
